package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import net.wesley.android.City;
import net.wesley.android.OnRequestParamsFinish;
import net.yebaihe.sdk.HttpConnection;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class TaiYuan extends City implements OnRequestParamsFinish {

	private String chejia;
	private String viewstate="";
	private Handler request1handler=new Handler(){


		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				int index=body.indexOf("id=\"__VIEWSTATE\" value=\"");
				if (index>0){
					viewstate=body.substring(index+24).split("\"")[0];
					dosearch2();
				}
				else{
					callback.onComplete(-10001, "");
				}
				break;
			}
		}
	};
	private int total;
	private Handler request2handler=new Handler(){

		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				int index=body.indexOf("alert('");
				if(index>0){
					String msg=body.substring(index+7).split("'\\)")[0];
					callback.onComplete(-10002, "ret:"+msg); 
				}
				else{
					index=body.indexOf("查到您最近有 ");
					if (index>0){
						String substring=body.substring(index+7).split(" 项未处理的违法")[0];
						Log.d("myown", "共有"+substring+"项："+body.substring(index));
						try{
							total=Integer.parseInt(substring);
							if (total==0){
								callback.onComplete(0, "");
								return;
							}
						}
						catch (Exception e){
							callback.onComplete(-10003, "");
						}
					}
					
					String str=body;
					str=str.split("id=\"GridView1\"")[1];
					str=str.split("</table>")[0];
					str=str.split("<th",2)[1];
					String[] rows=str.split("<tr");
					boolean firstrow=true;
					String retvalue="";
					for (int i=0;i<rows.length;i++){
						if (firstrow){
							firstrow=false;
							continue;
						}
						String row=rows[i];
						String[] cols = row.split("<td");
						String shijian = cols[4].split(">")[2].split("<")[0];
						String didian = cols[5].split(">")[2].split("<")[0];
						String code = cols[6].split(">")[2].split("<")[0];
						retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
					}
					callback.onComplete(total, retvalue);
				}
				break;
			}
		}
	};

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("晋A");
	}

	protected void dosearch2() {
		try {
			new HttpConnection(ctx,request2handler,"GBK").post("http://www.tyjj.gov.cn:82/illegal_query_result.aspx", 
					String.format("TextBox1=%s&TextBox5=%s&btnVehicle=&DropDownList2=%s&DropDownList1=%s&__VIEWSTATE=%s",
							chepai.substring(1),chejia,URLEncoder.encode("晋","GBK"),URLEncoder.encode("小型汽车","GBK"),URLEncoder.encode(viewstate)));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String[] paramDesc() {
		String[] s={"车架号后六位"};
		return s;
	}

	public void requestParamsFinish(String[] values) {
		if ((values.length==1) && (values[0].length()==6)){
			this.chejia=values[0];
			request1();
		}
	}

	private void request1() {
		new HttpConnection(ctx,request1handler,"GBK").get("http://www.tyjj.gov.cn/");
	}

}
